/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components;

import java.awt.AWTKeyStroke;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import javax.swing.JComponent;

/**
 *
 * @author glauco
 */
public class FocusTraversalKeys {

    /**
     * Adiciona a tecla VK_ENTER como ação para passar o focus para o próximo
     * campo.
     * Este método serve para todos os componentes do container, exceto
     * componentes de texto com multiplas linhas, como por exemplo JTextArea.
     * @param frame Container o qual a tecla VK_ENTER irá ser usada para passar
     * o focus adiante.
     * @see java.awt.event.KeyEvent#VK_ENTER
     */
    public static void setFocusTraversalKeys(Container frame) {
        HashSet hs = new HashSet(frame.getFocusTraversalKeys(
                                 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

        hs.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));

        frame.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hs);

    }

    /**
     * Adiciona a tecla VK_TAB como ação para passar o focus para o próximo
     * campo.
     * @param component o qual receberá a ação.
     * @see java.awt.event.KeyEvent#VK_TAB
     */
    public static void setComponentFocusTraversalKeys(JComponent component) {
        HashSet hs = new HashSet(component.getFocusTraversalKeys(
                                 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

        hs.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0));

        component.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, hs);

    }
}
